import util from '../../utils/util'
import {
  MessageBoard,
  MessageType
} from '../../config/api'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEmpty: false,
    type: '', // 栏目类型
    active: 0, //当前激活选中项，默认为0
    columns: [], //栏目数组
    MessageList: [], //留言列表
    page: 1,
    limit: 10,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    util.apiRequest(MessageType, {
      vollege_id: app.globalData.vollege_id
    }, null).then(r => {
      console.log('留言板类型：', r.data.data);
      if (r.data.data.length == 0) {
        that.setData({
          isEmpty: true
        })
        return
      }
      that.setData({
        columns: r.data.data,
        type: r.data.data[0].id
      });
      that.getList(); //  调用列表
    });
  },

  getList() { // 获取列表
    let that = this;
    wx.showLoading({
      title: '玩命加载中',
    })
    util.apiRequest(MessageBoard, {
      page: that.data.page,
      limit: that.data.limit,
      vollege_id: app.globalData.vollege_id,
      type: that.data.type
    }, null).then(r => {
      console.log('留言列表数据', r.data.data.data);
      var newList = that.data.MessageList.concat(r.data.data.data)

      if (r.data.data.data == '') {
        wx.showToast({
          title: '暂无更多',
          icon: 'none',
        })
      }
      that.setData({
        MessageList: newList
      })
      wx.hideLoading();

      if (newList.length == 0) {
        that.setData({
          isEmpty: true
        })
      } else {
        that.setData({
          isEmpty: false
        })
      }
    });
  },

  SwitchStatus: function (e) {
    // 传递的参数
    let index = e.currentTarget.dataset['index'];
    let type = e.currentTarget.dataset['type'];
    // console.log('点击事件传递的参数index：',index);
    // console.log('点击事件传递的参数type：',type);
    let that = this;
    that.setData({
      page: 1,
      MessageList: [],
      type,
      active: index,
    });

    that.getList();

  },

  // 跳转到内容页
  toNewsDetail: function (e) {
    let id = e.currentTarget.dataset['id'];
    let status = e.currentTarget.dataset['status'];
    console.log('新闻详情id:', id);
    wx.navigateTo({
      url: '/pages/message/msg_detail/msg_detail?id=' + id + '&status=' + status,
    })
  },

  onMsg: function () {
    // console.log('参数type：',this.data.columns[this.data.active].id);
    wx.navigateTo({
      url: '/pages/message/msg_form/msg_form',
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = this.data.page;
    page++;
    that.setData({
      page: page
    })
    console.log('page++', page)
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})